package com.odm.communitymanage.ui.mine.MyComplaint

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.ToastUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import com.odm.communitymanage.R
import com.odm.communitymanage.base.BaseMVVMFragment
import com.odm.communitymanage.core.Constants
import com.odm.communitymanage.core.bean.MyComplaints
import com.odm.communitymanage.core.net.ApiService
import com.rxjava.rxlife.lifeOnMain
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.fragment_my_complaint.*
import rxhttp.wrapper.param.RxHttp

/**
 * @description: 我的投诉 页面
 * @author: ODM
 * @date: 2020/4/15
 */
class MyComplaintFragment : BaseMVVMFragment() {

    var rvAdapter : MyComplaintAdapter ?= null

    var dispoable : Disposable ?= null


    override fun onDestroyView() {
        super.onDestroyView()
        if(dispoable ?.isDisposed == false){
            dispoable?.dispose()
        }
    }

    override fun initViewModelObserve() {

    }

    override fun initViews() {
        btnBackMyComplaints.setOnClickListener {
            pop()
        }

        tvPostMyComplaint.setOnClickListener {
            start(PostComplaintFragment())
        }
        //初始化 RecyclerView
        rvMyComplaints.layoutManager= LinearLayoutManager(requireContext())
        //初始化适配器
        rvAdapter = MyComplaintAdapter(mutableListOf())
        rvAdapter?.animationEnable = true
        rvMyComplaints?.adapter = rvAdapter
//        //设置空布局
//        rvAdapter?.setEmptyView(R.layout.layout_empty_list)
        //rv点击事件
        rvAdapter?.setOnItemClickListener{
                adapter, view, position ->
            LiveEventBus.get(Constants.EVENT_MY_COMPLAINT_DETAIL)
                .post(rvAdapter?.data?.get(position))
            start(MyComplaintDetailFragment())
        }
    }

    override fun getLayoutId(): Int {
        return  R.layout.fragment_my_complaint
    }


    override fun onSupportVisible() {
        super.onSupportVisible()
        getMyComplaintsData()
    }

    private fun getMyComplaintsData(){
        dispoable = RxHttp.get(ApiService.MY_COMPLAINT_LIST)
            .asClass(MyComplaints::class.java)
            .lifeOnMain(this)
            .subscribe({
                //返回数据有异常情况
                if(it.code != 1){
                    ToastUtils.showShort(it.message)
                    val data = it.data.reversed().toMutableList()
                    rvAdapter?.setNewData(data)
                }
                if(it?.data?.isNotEmpty() == true){
                    msv_my_complaint?.showContent()
                    rvAdapter?.setList(it.data.toMutableList())
                }else {
                    msv_my_complaint?.showEmpty()
                }
            },{
                ToastUtils.showShort(it.message)
            })
    }

}